/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package org.aroundme.unittests;

import java.util.ArrayList;

import org.aroundme.model.Location;
import org.aroundme.model.Place;
import org.aroundme.model.User;
import org.aroundme.persistency.LocationManager;
import org.aroundme.unitTools.TestFixture;
import org.aroundme.unitTools.UniqueIdGenerator;
import org.aroundme.utility.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Session;

public class LocationManagerTest extends TestFixture {
	private LocationManager locManager;
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		locManager = new LocationManager();
	}
	
	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		locManager = null;
	}
	
	public void testSaveLocation(){
		//create a dummy location
		Location loc = new Location();
		loc.setCellid(String.valueOf(new UniqueIdGenerator().getUniqueId()));
		loc.setLat("15,1");
		loc.setLon("20,7");
		
		//save it to the DB
		locManager.saveLocation(loc);
		
		//check if it is present..try to retrieve it
		Session s = HibernateUtil.getSessionFactory().getCurrentSession();
		Location locRetrieved = null;
		s.beginTransaction();
		try{
			locRetrieved = (Location) s.createQuery("from Location where id=?").setInteger(0, loc.getId()).uniqueResult();
			s.getTransaction().commit();
		}catch (HibernateException e){
			s.getTransaction().rollback();
			e.printStackTrace();
			fail(e.getMessage());
		}
		
		assertNotNull(locRetrieved);
		assertEquals("cellid", loc.getCellid(), locRetrieved.getCellid());
		assertEquals("lat, long", loc.getLat() + "," + loc.getLon(), locRetrieved.getLat() + "," + loc.getLon());
		
		//create a new Location and add places to it
		Location loc2 = new Location();
		loc2.setCellid(String.valueOf(new UniqueIdGenerator().getUniqueId()));
		loc2.setLat("20,5223");
		loc2.setLon("-15,24");
		
		
		User placeUs =  xmlTestUtils.getDummyUsers().get(0);
		xmlTestUtils.saveDummyUsersToDB();
		
		//add a series of dummy places to the location
		ArrayList<Place> places = xmlTestUtils.getDummyPlaces();
		for (Place place : places) {
			place.setUser(placeUs);
			loc2.addPlace(place);
		}
		
		//save the location
		locManager.saveLocation(loc2);
		
		//try to retrieve it again
		s = HibernateUtil.getSessionFactory().getCurrentSession();
		Location loc2Retrieved = null;
		s.beginTransaction();
		try{
			loc2Retrieved = (Location) s.createQuery("from Location where id=?").setInteger(0, loc2.getId()).uniqueResult();
			s.getTransaction().commit();
		}catch(HibernateException e){
			s.getTransaction().rollback();
			fail(e.getMessage());
			e.printStackTrace();
		}
		assertNotNull(loc2Retrieved);
		assertEquals("count of saved place-items", loc2.getPlaces().size(), loc2Retrieved.getPlaces().size());
		assertEquals("cellid of loc2", loc2.getCellid(), loc2Retrieved.getCellid());
	}
	
	public void testGetLocationWithCellId(){
//		create a new Location and add places to it
		Location loc = new Location();
		loc.setCellid(String.valueOf(new UniqueIdGenerator().getUniqueId()));
		loc.setLat("20,5223");
		loc.setLon("-15,24");
		
		User placeUs =  xmlTestUtils.getDummyUsers().get(0);
		xmlTestUtils.saveDummyUsersToDB();

		//add a series of dummy places to the location
		ArrayList<Place> places = xmlTestUtils.getDummyPlaces();
		for (Place place : places) {
			place.setUser(placeUs);
			loc.addPlace(place);
		}
		
		//save the location
		locManager.saveLocation(loc);
		
		//try to retrieve location
		Location locRetrieved = locManager.getLocationWithCellId(loc.getCellid());
		
		assertNotNull(locRetrieved);
		assertEquals("cellid of loc", loc.getCellid(), locRetrieved.getCellid());
		assertEquals("count of saved place-items", loc.getPlaces().size(), locRetrieved.getPlaces().size());
	}
	
	private void saveTestSet(User placeUs) {
		Session s = HibernateUtil.getSessionFactory().getCurrentSession();
		s.beginTransaction();
		try{
			s.saveOrUpdate(placeUs);
			s.getTransaction().commit();
		}catch(HibernateException e){
			s.getTransaction().rollback();
			e.printStackTrace();
			fail(e.getMessage());
		}
	}
}
